package com.ruix.app.ui.adapter;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ruix.app.R;
import com.ruix.app.adapter.superadapter.IMulItemViewType;
import com.ruix.app.adapter.superadapter.OnItemClickListener;
import com.ruix.app.adapter.superadapter.SuperAdapter;
import com.ruix.app.adapter.superadapter.internal.SuperViewHolder;
import com.ruix.app.manager.UserManager;
import com.ruix.app.model.Comment;
import com.ruix.app.model.PostsCard;
import com.ruix.app.model.PostsType;
import com.ruix.app.ui.stratum.notice.ReleaseNoiceActivity;
import com.ruix.app.util.GlideUitl;
import com.ruix.app.widget.MultiImageViewLayout;
import com.ruix.app.widget.linearlistview.LinearListView;
import com.ruix.app.widget.praise.PraiseTextView;
import com.ruix.app.widget.praise.PraiseWidget;

import java.util.ArrayList;
import java.util.List;

import me.iwf.photopicker.PhotoPreview;

/**
 * Created by hadexs on 2017/4/22.
 */

public class PracticalCommuAdapter extends SuperAdapter<PostsCard>{


    private CommentAdapter commentAdapter;
    private PracticalCommuActionListenler listenler;
    private String userId;
    private Activity activity;
    private boolean canShare;
    private boolean hasMoreAction;
    private boolean canDelete;
    private boolean prohibited;

    public PracticalCommuAdapter(Context context, List<PostsCard> items, IMulItemViewType<PostsCard> mulItemViewType) {
        super(context, items, mulItemViewType);
        userId = UserManager.getCurrentUser().getID();
        canDelete = true;
        hasMoreAction = true;
        commentAdapter = new CommentAdapter(context);
    }

    @Override
    public void onBind(final SuperViewHolder holder, int viewType, final int position, final PostsCard item) {
        holder.setSimpleDraweeURI(R.id.iv_auth_avatar,item.getAvatarUrl());
        holder.setText(R.id.tv_auth_name,item.getUserName());
        holder.setText(R.id.tv_create_time,item.getCreatedTimeDisplay());


        holder.setOnClickListener(R.id.iv_auth_avatar, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listenler != null){
                    listenler.clickAvatar(item,position);
                }

            }
        });

        //角色
       String roleValue =  item.authorRoleValue(false);
        if (roleValue != null){
            holder.setText(R.id.auther_role,roleValue);
            holder.setVisibility(R.id.auther_role,View.VISIBLE);
        } else {
            holder.setVisibility(R.id.auther_role,View.GONE);
        }


        if (item.isHasPraisedByUser()){
            holder.setImageViewSrc(R.id.btn_good_up,R.drawable.like_praised);
        } else {
            holder.setImageViewSrc(R.id.btn_good_up,R.drawable.like_normal);
        }

        // 点赞
        ArrayList list =  item.getLikes();
        if (list != null && list.size() > 0){
            PraiseTextView widget =  holder.getView(R.id.tw_praise_widget);
            widget.setData(list);
            holder.setVisibility(R.id.tw_praise_widget, View.VISIBLE);
        } else {
            holder.setVisibility(R.id.tw_praise_widget, View.GONE);
        }

        //点赞
        holder.setOnClickListener(R.id.btn_good_up, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listenler != null) {
                    if (item.isHasPraisedByUser()){
                        listenler.unpraisePosts(item,position);
                    } else {
                        listenler.praisePosts(item,position);
                    }
                }
            }
        });

        //评论
        holder.setOnClickListener(R.id.btn_comment_posts, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listenler != null) {
                    listenler.commentPosts(item,position);
                }
            }
        });
        if (prohibited){ //禁言不能评论
            holder.setVisibility(R.id.btn_comment_posts,View.GONE);
        } else {
            holder.setVisibility(R.id.btn_share_posts,View.VISIBLE);
        }


        //分享
        holder.setOnClickListener(R.id.btn_share_posts, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listenler != null) {
                    listenler.sharePosts(item);
                }
            }
        });
        if (canShare){
            holder.setVisibility(R.id.btn_share_posts,View.VISIBLE);
        } else {
            holder.setVisibility(R.id.btn_share_posts,View.GONE);
        }

        //删除
        if (item.getUid().equals(userId)){
            holder.setVisibility(R.id.btn_detele_posts,View.VISIBLE);
            holder.setOnClickListener(R.id.btn_detele_posts, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listenler != null) {
                        listenler.deletePosts(item,position);
                    }
                }
            });
        } else {
            holder.setVisibility(R.id.btn_detele_posts,View.GONE);
        }

        if (!canDelete){
            holder.setVisibility(R.id.btn_detele_posts,View.GONE);
        }

        //更多
        holder.setOnClickListener(R.id.btn_more_action, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listenler != null) {
                    listenler.moreActionPosts(item);
                }
            }
        });
        if (!hasMoreAction){
            holder.setVisibility(R.id.btn_more_action,View.GONE);
        }


        // 文本
        if (!TextUtils.isEmpty(item.getWords())) {
            holder.setText(R.id.tv_content_view, item.getWords());
            TextView view =  holder.getView(R.id.tv_content_view);
            holder.setVisibility(R.id.tv_content_view, View.VISIBLE);
            final TextView contentView = holder.getView(R.id.tv_content_view);
            int count = contentView.getLineCount();
            if (count > 15) {
                holder.setVisibility(R.id.btn_all_text, View.VISIBLE);
                holder.setVisibility(R.id.btn_pick_up_text, View.GONE);
                holder.setOnClickListener(R.id.btn_all_text, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        contentView.setMaxLines(400);
                        holder.setVisibility(R.id.btn_all_text, View.GONE);
                        holder.setVisibility(R.id.btn_pick_up_text, View.VISIBLE);
                    }
                });
                holder.setOnClickListener(R.id.btn_pick_up_text, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        contentView.setMaxLines(15);
                        holder.setVisibility(R.id.btn_all_text, View.VISIBLE);
                        holder.setVisibility(R.id.btn_pick_up_text, View.GONE);
                    }
                });
            } else {
                holder.setVisibility(R.id.btn_all_text, View.GONE);
            }
        } else {
            holder.setVisibility(R.id.tv_content_view, View.GONE);
            holder.setVisibility(R.id.btn_all_text, View.GONE);
            holder.setVisibility(R.id.btn_pick_up_text, View.GONE);
        }
        //图片
//        if (viewType == PostsType.POSTS_TYPE_PIC.ordinal()){
//            MultiImageViewLayout multiImageView =  holder.getView(R.id.multiimage_layout);
//            multiImageView.setList(item.getAlbum());
//        }
//        else
            if (viewType == PostsType.POSTS_TYPE_ONE.ordinal()) {
            //
//            holder.get
            ImageView picOne = holder.getView(R.id.iv_pic_one);
            GlideUitl.showImageView(item.getAlbum().get(0) + "",picOne);
//            holder.setSimpleDraweeURI(R.id.iv_pic_one,item.getAlbumWithIndex(0));
            holder.setOnClickListener(R.id.iv_pic_one, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //todo
                    PhotoPreview.builder()
                            .setPhotos(item.getAlbum())
                            .setCurrentItem(0)
                            .setShowDeleteButton(false)
                            .start(activity);
                }
            });



//            MultiImageViewLayout multiImageView =  holder.getView(R.id.multiimage_layout);
//            multiImageView.setList(item.getAlbum());
        } else if (viewType == PostsType.POSTS_TYPE_TWO.ordinal()) {
            holder.setSimpleDraweeURI(R.id.iv_pic_one,item.getAlbumWithIndex(0));
            holder.setOnClickListener(R.id.iv_pic_one, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //todo
                    PhotoPreview.builder()
                            .setPhotos(item.getAlbum())
                            .setCurrentItem(0)
                            .setShowDeleteButton(false)
                            .start(activity);
                }
            });
            holder.setSimpleDraweeURI(R.id.iv_pic_two,item.getAlbumWithIndex(1));
            holder.setOnClickListener(R.id.iv_pic_two, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //todo
                    PhotoPreview.builder()
                            .setPhotos(item.getAlbum())
                            .setCurrentItem(1)
                            .setShowDeleteButton(false)
                            .start(activity);
                }
            });
        } else if (viewType == PostsType.POSTS_TYPE_THREE.ordinal()) {
            holder.setSimpleDraweeURI(R.id.iv_pic_one,item.getAlbumWithIndex(0));
            holder.setSimpleDraweeURI(R.id.iv_pic_two,item.getAlbumWithIndex(1));
            holder.setSimpleDraweeURI(R.id.iv_pic_three,item.getAlbumWithIndex(2));

            holder.setOnClickListener(R.id.iv_pic_one, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PhotoPreview.builder()
                            .setPhotos(item.getAlbum())
                            .setCurrentItem(0)
                            .setShowDeleteButton(false)
                            .start(activity);
                }
            });
            holder.setOnClickListener(R.id.iv_pic_two, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PhotoPreview.builder()
                            .setPhotos(item.getAlbum())
                            .setCurrentItem(1)
                            .setShowDeleteButton(false)
                            .start(activity);
                }
            });
            holder.setOnClickListener(R.id.iv_pic_three, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PhotoPreview.builder()
                            .setPhotos(item.getAlbum())
                            .setCurrentItem(2)
                            .setShowDeleteButton(false)
                            .start(activity);
                }
            });



        } else if (viewType == PostsType.POSTS_TYPE_FOUR.ordinal()) {
            holder.setSimpleDraweeURI(R.id.iv_pic_one,item.getAlbumWithIndex(0));
            holder.setSimpleDraweeURI(R.id.iv_pic_two,item.getAlbumWithIndex(1));
            holder.setSimpleDraweeURI(R.id.iv_pic_three,item.getAlbumWithIndex(2));
            holder.setSimpleDraweeURI(R.id.iv_pic_four,item.getAlbumWithIndex(3));
            holder.setOnClickListener(R.id.iv_pic_one, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PhotoPreview.builder()
                            .setPhotos(item.getAlbum())
                            .setCurrentItem(0)
                            .setShowDeleteButton(false)
                            .start(activity);
                }
            });
            holder.setOnClickListener(R.id.iv_pic_two, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PhotoPreview.builder()
                            .setPhotos(item.getAlbum())
                            .setCurrentItem(1)
                            .setShowDeleteButton(false)
                            .start(activity);
                }
            });
            holder.setOnClickListener(R.id.iv_pic_three, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PhotoPreview.builder()
                            .setPhotos(item.getAlbum())
                            .setCurrentItem(2)
                            .setShowDeleteButton(false)
                            .start(activity);
                }
            });
            holder.setOnClickListener(R.id.iv_pic_four, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PhotoPreview.builder()
                            .setPhotos(item.getAlbum())
                            .setCurrentItem(3)
                            .setShowDeleteButton(false)
                            .start(activity);
                }
            });

        } else if (viewType == PostsType.POSTS_TYPE_FIVE.ordinal()) {
            holder.setSimpleDraweeURI(R.id.iv_pic_one,item.getAlbumWithIndex(0));
            holder.setSimpleDraweeURI(R.id.iv_pic_two,item.getAlbumWithIndex(1));
            holder.setSimpleDraweeURI(R.id.iv_pic_three,item.getAlbumWithIndex(2));
            holder.setSimpleDraweeURI(R.id.iv_pic_four,item.getAlbumWithIndex(3));
            holder.setSimpleDraweeURI(R.id.iv_pic_five,item.getAlbumWithIndex(4));
            if (item.getAlbum().size() == 6){
                holder.setSimpleDraweeURI(R.id.iv_pic_six,item.getAlbumWithIndex(5));
                holder.setVisibility(R.id.iv_pic_six, View.VISIBLE);
                holder.setOnClickListener(R.id.iv_pic_six, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //todo
                        PhotoPreview.builder()
                                .setPhotos(item.getAlbum())
                                .setCurrentItem(5)
                                .setShowDeleteButton(false)
                                .start(activity);
                    }
                });
            } else {
                holder.setVisibility(R.id.iv_pic_six, View.GONE);
            }
            holder.setOnClickListener(R.id.iv_pic_one, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PhotoPreview.builder()
                            .setPhotos(item.getAlbum())
                            .setCurrentItem(0)
                            .setShowDeleteButton(false)
                            .start(activity);
                }
            });
            holder.setOnClickListener(R.id.iv_pic_two, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PhotoPreview.builder()
                            .setPhotos(item.getAlbum())
                            .setCurrentItem(1)
                            .setShowDeleteButton(false)
                            .start(activity);
                }
            });
            holder.setOnClickListener(R.id.iv_pic_three, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PhotoPreview.builder()
                            .setPhotos(item.getAlbum())
                            .setCurrentItem(2)
                            .setShowDeleteButton(false)
                            .start(activity);
                }
            });
            holder.setOnClickListener(R.id.iv_pic_four, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PhotoPreview.builder()
                            .setPhotos(item.getAlbum())
                            .setCurrentItem(3)
                            .setShowDeleteButton(false)
                            .start(activity);
                }
            });

            holder.setOnClickListener(R.id.iv_pic_five, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PhotoPreview.builder()
                            .setPhotos(item.getAlbum())
                            .setCurrentItem(4)
                            .setShowDeleteButton(false)
                            .start(activity);
                }
            });

        } else if (viewType == PostsType.POSTS_TYPE_SIX.ordinal()) {
            holder.setSimpleDraweeURI(R.id.iv_pic_one,item.getAlbumWithIndex(0));
            holder.setSimpleDraweeURI(R.id.iv_pic_two,item.getAlbumWithIndex(1));
            holder.setSimpleDraweeURI(R.id.iv_pic_three,item.getAlbumWithIndex(2));
            holder.setSimpleDraweeURI(R.id.iv_pic_four,item.getAlbumWithIndex(3));
            holder.setSimpleDraweeURI(R.id.iv_pic_five,item.getAlbumWithIndex(4));
            holder.setSimpleDraweeURI(R.id.iv_pic_six,item.getAlbumWithIndex(5));
            holder.setSimpleDraweeURI(R.id.iv_pic_seven,item.getAlbumWithIndex(6));
            if (item.getAlbum().size() == 8){
                holder.setSimpleDraweeURI(R.id.iv_pic_eight,item.getAlbum().get(7));
                holder.setVisibility(R.id.iv_pic_eight, View.VISIBLE);
                holder.setVisibility(R.id.iv_pic_nine, View.GONE);
                holder.setOnClickListener(R.id.iv_pic_eight, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        PhotoPreview.builder()
                                .setPhotos(item.getAlbum())
                                .setCurrentItem(7)
                                .setShowDeleteButton(false)
                                .start(activity);
                    }
                });
            } else if (item.getAlbum().size() == 9){
                holder.setSimpleDraweeURI(R.id.iv_pic_eight,item.getAlbum().get(7));
                holder.setSimpleDraweeURI(R.id.iv_pic_nine,item.getAlbum().get(8));
                holder.setVisibility(R.id.iv_pic_nine, View.VISIBLE);
                holder.setVisibility(R.id.iv_pic_eight, View.VISIBLE);
                holder.setOnClickListener(R.id.iv_pic_eight, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        PhotoPreview.builder()
                                .setPhotos(item.getAlbum())
                                .setCurrentItem(7)
                                .setShowDeleteButton(false)
                                .start(activity);
                    }
                });
                holder.setOnClickListener(R.id.iv_pic_nine, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        PhotoPreview.builder()
                                .setPhotos(item.getAlbum())
                                .setCurrentItem(8)
                                .setShowDeleteButton(false)
                                .start(activity);
                    }
                });
            } else {
                holder.setVisibility(R.id.iv_pic_nine, View.GONE);
                holder.setVisibility(R.id.iv_pic_eight, View.GONE);
            }

            holder.setOnClickListener(R.id.iv_pic_one, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PhotoPreview.builder()
                            .setPhotos(item.getAlbum())
                            .setCurrentItem(0)
                            .setShowDeleteButton(false)
                            .start(activity);
                }
            });
            holder.setOnClickListener(R.id.iv_pic_two, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PhotoPreview.builder()
                            .setPhotos(item.getAlbum())
                            .setCurrentItem(1)
                            .setShowDeleteButton(false)
                            .start(activity);
                }
            });
            holder.setOnClickListener(R.id.iv_pic_three, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PhotoPreview.builder()
                            .setPhotos(item.getAlbum())
                            .setCurrentItem(2)
                            .setShowDeleteButton(false)
                            .start(activity);
                }
            });
            holder.setOnClickListener(R.id.iv_pic_four, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PhotoPreview.builder()
                            .setPhotos(item.getAlbum())
                            .setCurrentItem(3)
                            .setShowDeleteButton(false)
                            .start(activity);
                }
            });

            holder.setOnClickListener(R.id.iv_pic_five, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PhotoPreview.builder()
                            .setPhotos(item.getAlbum())
                            .setCurrentItem(4)
                            .setShowDeleteButton(false)
                            .start(activity);
                }
            });
            holder.setOnClickListener(R.id.iv_pic_six, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PhotoPreview.builder()
                            .setPhotos(item.getAlbum())
                            .setCurrentItem(5)
                            .setShowDeleteButton(false)
                            .start(activity);
                }
            });
            holder.setOnClickListener(R.id.iv_pic_seven, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PhotoPreview.builder()
                            .setPhotos(item.getAlbum())
                            .setCurrentItem(6)
                            .setShowDeleteButton(false)
                            .start(activity);
                }
            });

        }

        //评论
        final ArrayList<Comment> comments = item.getComments();
        if (comments != null &&comments.size() > 0){
            holder.setVisibility(R.id.llv_comments_details, View.VISIBLE);
            final LinearListView linearListView = (LinearListView)holder.getView(R.id.llv_comments_details);
            linearListView.setAdapter(commentAdapter);//todo
            commentAdapter.replaceAll(comments);
            linearListView.setOnItemClickListener(new LinearListView.OnItemClickListener() {
                @Override
                public void onItemClick(LinearListView parent, View view, int position, long id) {
                    if (listenler != null){
                        listenler.actionComment(item,position);
                    }
                }

                @Override
                public void onItemLongClick(LinearListView parent, View view, int position, long id) {
                    if (listenler != null){
                        listenler.onLongClickComment(item,position);
                    }
                }
            });
        } else {
            holder.setVisibility(R.id.llv_comments_details, View.GONE);
        }

//        llv_comments_details


    }

    public void setActivity(Activity activity) {
        this.activity = activity;
    }

    public interface PracticalCommuActionListenler{
        void deletePosts(PostsCard item,int position);
        void sharePosts(PostsCard item);
        void commentPosts(PostsCard item,int position);
        void moreActionPosts(PostsCard item);
        void praisePosts(PostsCard item,int position);
        void unpraisePosts(PostsCard item,int position);
        void actionComment(PostsCard item,int commentPosition); //点击评论
        void onLongClickComment(PostsCard item,int commentPosition); //长按评论
        void clickAvatar(PostsCard item,int position); //点击头像
    }

    public void setActionListenler(PracticalCommuActionListenler listenler) {
        this.listenler = listenler;
    }

    public void setCanShare(boolean canShare) {
        this.canShare = canShare;
    }

    public void setHasMoreAction(boolean hasMoreAction) {
        this.hasMoreAction = hasMoreAction;
    }

    public void setCanDelete(boolean canDelete) {
        this.canDelete = canDelete;
    }

    public void setProhibited(boolean prohibited) {
        this.prohibited = prohibited;
    }
}
